﻿#include "nuf_bus_info.h"
#include "dialog_nuf_bus_setting.h"
#include "ui_dialog_nuf_bus_setting.h"

#include <QComboBox>
#include <QLabel>
#include <QVBoxLayout>

#include "com_manager.h"

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

DialogNufBusSetting::DialogNufBusSetting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogNufBusSetting)
{
    ui->setupUi(this);

    setWindowTitle("NufBus 设置");
    QVBoxLayout *vLayout = new QVBoxLayout;
    
    
    int cnt = NufBusSetting::map_bus.count();
    for ( int i = 0; i < cnt; i ++)
    {
        QHBoxLayout * hLayout = new QHBoxLayout;
        hLayout->addWidget(new QLabel(QString("NufBus %1").arg(i)));
        QComboBox *comboBox = new QComboBox;
        comboBox->addItems(com_manager::instance()->comList);
        hLayout->addWidget(comboBox);

        QMetaObject::Connection con=
        connect(comboBox, &QComboBox::currentTextChanged, [=](QString com)
        {
            NufBusSetting::map_bus[i].port_name = com;
        });
        
        comboBox->setCurrentText( NufBusSetting::map_bus[i].port_name );

        //当配置文件中配置的串口不存在时，设为第一个有效串口
        NufBusSetting::map_bus[i].port_name = comboBox->currentText();

        vLayout->addItem(hLayout);
    }

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addItem(vLayout);

    ui->frame ->setLayout(layout);

}

DialogNufBusSetting::~DialogNufBusSetting()
{
    delete ui;
}

void DialogNufBusSetting::on_buttonBox_accepted()
{
    
    
    NufBusSetting::save_Bus_Setting();
}
